

require "TSLib"
init(0)


build_spacing = 150;  -- x y  通用

-- allow_search_path_count = 5;  -- 可搜索路径
build_color = 0x3a944a;
error_count = 0;  -- 错误计数
tap_left_x = 42;
tap_left_y = 1458;
tap_right_x = 986;
tap_right_y = 1458;
obstacle_color = 0x579b3b; -- 障碍物
obstacle_spacing_y = 100;  -- 障碍物间距
obstacle_spacing_x = 20;
w,h = getScreenSize();
x= -1;
y= -1;
original_color = 0x3a944a; -- 源点

-- left --right
function search_path(x,y,last_path) 
    local lx = x-build_spacing;
    local ly = y-build_spacing;
    local rx = x+build_spacing;
    local ry = y-build_spacing;
    local enable_left = false;
    local enable_right = false;
    local enable_left_an_right = false;

    if isColor(lx,ly,build_color) == true and (isColor(lx+obstacle_spacing_y, ly-obstacle_spacing_y, obstacle_color,85) == false) then
        enable_left = true;
        
    end

    if isColor(rx,ry,build_color) == true and (isColor(rx-obstacle_spacing_x, ry-obstacle_spacing_y, obstacle_color,85) == false) then
        enable_right = true;
        
    end

    if enable_left == true and enable_right == true then
        log("[DATE][LINE][PATH]".."enable_left_an_right","test日志")
        return search_path(lx,ly,0);
    elseif enable_left == true then
        log("[DATE][LINE][PATH]".."enable_left","test日志")
        return 0;
    elseif enable_right == true then
        log("[DATE][LINE][PATH]".."enable_right","test日志")
        return 1;
    elseif last_path == 1 then
        log("[DATE][LINE][PATH]".."enable_left","test日志")
        return 0;
    elseif last_path ==0 then
        log("[DATE][LINE][PATH]".."enable_right","test日志")
        return 1;
    else
        return -1;
    end

    
end


function loop_exec_script() 

    -- 角色方向朝左
if x == -1 or y == -1 then
x,y = findMultiColorInRegionFuzzy( 0x3a944a, "-23|-88|0xf9e0d0,0|-90|0xb72f1e,18|-90|0xe5c4c4", 90, 414, 978, 662, 1215)
end

if x == -1 or y == -1 then
    -- 角色方向朝右
    x,y = findMultiColorInRegionFuzzy( 0x3a944a, "23|-88|0xf9e0d0,0|-90|0xb72f1e,-18|-90|0xe5c4c4", 90, 414, 978, 662, 1215)
    
end


-- 找不到角色直接停止运行
if  x == -1 or y == -1 then
    log("[DATE][LINE][PATH]".."未发现角色","test日志")
    return
else
    exec_path = {}; -- 路径 0 left , 1 right, -1 无路可走
    x= 540;
    y=1150;
    if isColor(x,y,original_color) == false then
        log("[DATE][LINE][PATH]".."角色位置未重置","test日志")
        return;
    end


    while true do
        local exec_res = -1;
        -- toast(string.format('x:%d , y :%d',x ,y));
        -- mSleep(500);
        -- table.insert(exec_path, search_path(x,y,nil));
        exec_res = search_path(x,y,nil);
        log("[DATE][LINE][PATH]".."result values: "..exec_res,"test日志")
        if exec_res == 1 then
            log("[DATE][LINE][PATH]".."result_1","test日志")
            x = x + build_spacing;
            y = y - build_spacing;
            error_count = 0;
        elseif exec_res == 0 then
            log("[DATE][LINE][PATH]".."result_0","test日志")
            x = x - build_spacing;
            y = y - build_spacing;
            error_count = 0;
        elseif error_count >= 5 then 
            current_time = os.date("%Y-%m-%d-%H-%M-%S", os.time());
            snapshot(current_time.."错误.png", 0, 0, w-1, h-1);
            log("[DATE][LINE][PATH]"..string.format('未进行点击错误%d次 -- 文件：%s',error_count, current_time.."错误.png"),"test日志")
            error_count = 0;
            break
        end;
        table.insert(exec_path, exec_res);
    end

    

    for i, v in ipairs(exec_path) do
        if v == 0 then
            tap(tap_left_x,tap_left_y,50);
        elseif v == 1 then
            tap(tap_right_x,tap_right_y,50);
        end
    end 
    x= 540;
    y=1150; 
end


end



while true do
    loop_exec_script();
    mSleep(200);
end 